/**
 * 获取天猫/拼多多店铺数据列表
 * @param vue {Vue} 当前页面对象 即 this
 * @param params {Object} 函数参数
 * @param success {function} 请求成功回调方法
 * @param finallyFuc {function} 请求的最后回调函数
 * */
export function storeList(vue, params, success, finallyFuc = f => {
}) {
  vue.$request.post({
    url: "store/tb/list",
    params: {},
    success: success,
    finally: finallyFuc,
    error: err => {
      console.log(err);
    }
  });
}

/**
 * 批量移除店铺数据列表
 * @param vue {Vue} 当前页面对象 即 this
 * @param idJsonList {Array} 移除数据id列表
 * @param success {function} 请求成功回调方法
 * */
export function batchRemove(vue, idJsonList, success) {
  vue.$request.post({
    url: "store/tb/del",
    params: {
      ids: idJsonList
    },
    success: success,
    error: (res) => {
      console.log(res);
    }
  });
}

export function getStoreOptions(vue, success, finallyFuc = r => {
}) {
  vue.$request.post({
    url: "store/tb/storeOptions",
    success: success,
    allError: r => {
      console.log(r);
    },
    finally: finallyFuc
  });
}

export function getDataDateOptions(vue, success, finallyFuc = r => {
}) {
  vue.$request.post({
    url: "store/tb/dataDateOptions",
    success: function(r) {
      success(r.map(i => formatDateToDay(i)));
    },
    allError: r => {
      console.log(r);
    },
    finally: finallyFuc
  });
}

export function getOneDayComparison(vue, params, success, finallyFuc = r => {
}) {
  vue.$request.post({
    url: "store/tb/oneDayComparison",
    params: params,
    success: success,
    allError: r => {
      console.log(r);
    },
    finally: finallyFuc
  });
}

export function getSameDateComparison(vue, params, success, finallyFuc = r => {
}) {
  vue.$request.post({
    url: "store/tb/sameDateComparison",
    params: params,
    success: success,
    allError: r => {
      console.log(r);
    },
    finally: finallyFuc
  });
}

export function formatOneDayComparison(res) {
  const result = [];
  const keys = Object.keys(res.diffPercentage);
  for (const key of keys) {
    const entry = {
      "field": oneDayComparisonFieldLabel[key],
      "diffPercentage": res.diffPercentage[key],
      "diffValue": res.diffValue[key],
      "leftData": res.leftData[key],
      "rightData": res.rightData[key]
    };
    result.push(entry);
  }
  return result;
}

/**
 * 分析列表至表格数据
 * @param list {Array} 列表数据
 * @param contrastUnit {string} 对比单位 ["日", "周", "月"]
 * @param contrastRelation {string} 对比关系 ["同期", "邻期"]
 * */
export function analysisListToTable(list,contrastUnit,contrastRelation){
  if (list.length === 0){
    return null
  }
  if (contrastRelation === "同期"){
    return tqFormatData(list,contrastUnit)
  }
  return null
}

/**
 * 同期解析
 * */
function tqFormatData(list,unit){
  // 初始化列名
  let namePrefix = unit === '日' ? " 较上周同期" : ""
  let columns = list.map(i => {
    return {
      label: formatDateToDay(i.dataDate) + namePrefix,
      width: "220px",
      fixed: false,
    }
  })
  // 修改数值结构
  let newList = []
  for(let key of Object.keys(list[0])){
    let name = oneDayComparisonFieldLabel[key]
    // 非指数字段跳出
    if (!name){
      continue
    }
    // 加上首位指数名
    let entry = {
      "指数名" : name,
    }
    columns.forEach((item,index)=> {
      // 对 对应列名 赋 对应的值 此时 key 固定 即这在表格模型中是对某一行进行赋值
      entry[item.label] = list[index][key]
    })
    newList.push(entry)
  }
  columns.unshift({label: "指数名",width: "140px", fixed: 'left',})
  return {
    columns : columns,
    newList : newList,
  }
}

/**
 * 计算两个值相差的百分比 返回百分比以及差值
 * */
export function calculateDiffPer(v1,v2){
  let diff = v1 - v2
  let scale  = v2 !== 0 ? diff / v2 : diff
  return {
    diff : diff.toFixed(2),
    per: (scale * 100).toFixed(2)
  }
}

/**
 * 格式化为 2020-01-01
 * */
export function formatDateToDay(v) {
  const date = new Date(v);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");

  return `${year}-${month}-${day}`;
}



const oneDayComparisonFieldLabel = {
  "amountPaid": "支付金额",
  "liveRoomAmount": "直播间金额",
  "payTransformRate": "支付转化率",
  "refundAmount": "退款金额",
  "visitorsNum": "访客数量",
  "wxtSpend": "万象台花费",
  "wxtTransactionAmount": "万象台成交额",
  "ylmfSpend": "引力魔方花费",
  "ylmfTransactionAmount": "引力魔方成交额",
  "ztcSpend": "直通车花费",
  "fillOrderNum": "补单数",
  "ztcTransactionAmount": "直通车成交额"
};